import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:mall/core/models/me.dart';
import 'package:mall/core/providers/pages/mine/user_qr_model.dart';
import 'package:mall/core/providers/view_model.dart';
import 'package:provider/provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:share/share.dart';

import '../../theme.dart';

class InvitePage extends StatelessWidget {
  static const routeName = '/InvitePage';
  final Me args;

  InvitePage({
    Key key,
    @required this.args,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider.value(value: args),
        ChangeNotifierProvider(
          create: (context) => UserQrModel(Provider.of(context, listen: false)),
        ),
      ],
      child: Theme(
        data: darkTheme(),
        child: Scaffold(
          appBar: AppBar(
            title: Text('邀请码'),
          ),
          body: Consumer<UserQrModel>(
            builder: (context, model, child) {
              switch (model.viewState) {
                case ViewState.busy:
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                case ViewState.error:
                  return Center(
                    child: Text(model.errorMessage),
                  );
                case ViewState.success:
                  return _SuccessWidget();
                default:
                  return Container();
              }
            },
          ),
        ),
      ),
    );
  }
}

class _SuccessWidget extends StatelessWidget {
  const _SuccessWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Me me = Provider.of(context, listen: false);
    UserQrModel model = Provider.of(context, listen: false);
    return SingleChildScrollView(
      child: Stack(
        children: <Widget>[
          Container(
            margin: EdgeInsets.symmetric(
              vertical: 45,
              horizontal: 17,
            ),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(16)),
            ),
            height: 520,
          ),
          Column(
            children: <Widget>[
              CircleAvatar(
                radius: 53.5,
                backgroundImage: CachedNetworkImageProvider(me.avatar ?? ''),
              ),
              Container(
                margin: EdgeInsets.only(top: 9),
                child: Text(
                  '${me.username}',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 16,
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 11, bottom: 4),
                child: Text(
                  '扩大资深财富',
                  style: TextStyle(color: Color(0xFFB0B0B0), fontSize: 14),
                ),
              ),
              Divider(
                height: 16,
                indent: 32,
                endIndent: 32,
                color: Colors.white,
              ),
              _CopyButtonWidget(),
              Container(
                padding: EdgeInsets.all(20),
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/user/img_qr.png'),
                    fit: BoxFit.fill,
                  ),
                ),
                width: 245,
                height: 245,
                margin: EdgeInsets.only(top: 15),
                child: QrImage(
                  data: model.url,
                ),
              ),
              _DownButtonWidget()
            ],
          )
        ],
      ),
    );
  }
}

class _CopyButtonWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    UserQrModel model = Provider.of(context, listen: false);
    return InkWell(
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16),
        decoration: BoxDecoration(
          color: Colors.black12,
          border: Border.all(color: Theme.of(context).primaryColor, width: 0.5),
          borderRadius: BorderRadius.all(Radius.circular(8)),
        ),
        height: 36,
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(
              '${model.inviteCode}',
              style: TextStyle(color: Colors.black),
            ),
            VerticalDivider(
              width: 24,
              thickness: 1,
              indent: 8,
              endIndent: 8,
              color: Theme.of(context).primaryColor,
            ),
            Text(
              '复制',
              style: TextStyle(color: Theme.of(context).primaryColor),
            ),
          ],
        ),
      ),
      onTap: () => _onCopyClicked(context),
    );
  }

  void _onCopyClicked(BuildContext context) async {
    UserQrModel model = Provider.of(context, listen: false);
    await Clipboard.setData(ClipboardData(text: model.inviteCode));
    EasyLoading.showToast('已复制');
  }
}

class _DownButtonWidget extends StatelessWidget {
  const _DownButtonWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: Container(
        decoration: BoxDecoration(
          color: Theme.of(context).primaryColor,
          borderRadius: BorderRadius.all(Radius.circular(4)),
        ),
        margin: EdgeInsets.only(top: 20),
        constraints: BoxConstraints(minWidth: 198, minHeight: 42),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Image.asset(
              'assets/images/user/ic_qr_code.png',
              width: 18,
              color: Colors.white,
            ),
            SizedBox(
              width: 5,
            ),
            Text('立即邀请好友')
          ],
        ),
      ),
      onTap: () {
        _onShareClicked(context);
      },
    );
  }

  void _onShareClicked(BuildContext context) {
    UserQrModel model = Provider.of(context, listen: false);
    Share.share(model.url, subject: '我的邀请码');
  }
}
